//
//  RGTabBarViewController.m
//  RedGrass
//
//  Created by wukong on 2018/11/24.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGTabBarViewController.h"
#import "BaseNavigationViewController.h"
#import "XYTabBar.h"
#import "UITabBar+CustomBadge.h"
#import "RGMainPageViewController.h"
#import "RGFocusViewController.h"
#import "RGVedioBlankViewController.h"
#import "VideoRecordViewController.h"
#import "RGMessageViewController.h"
#import "RGMeViewController.h"
#import "UIImage+Color.h"
#import "VideoPreviewViewController.h"
#import "VideoRecordConfig.h"
#import "VideoEditViewController.h"
#import "VideoCompressPreviewController.h"
#import "RGTabBarViewController+RedPackets.h"

@interface RGTabBarViewController ()<UITabBarControllerDelegate>

@property (nonatomic,strong) NSMutableArray * VCS;//tabbar root VC
@property (nonatomic,strong) XYTabBar *tabBar1;

@property (nonatomic,assign) BOOL isClear;

@end

@implementation RGTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    [[UITabBar appearance] setTranslucent:YES];
    //初始化tabbar
    [self setUpTabBar];
    //添加子控制器
    [self setUpAllChildViewController];
    //添加z底部横线
    [_tabBar1 addBottomLine];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SRWebSocketDidOpen) name:kWebSocketDidOpenNote object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SRWebSocketDidReceiveMsg:) name:kWebSocketdidReceiveMessageNote object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SRWebSocketDidClose:) name:kWebSocketDidCloseNote object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self connectWebSocketServer];
}

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        [self showRedPacketView];
//    });
}


#pragma mark ————— 初始化TabBar —————
-(void)setUpTabBar{
    //设置背景色 去掉分割线
   _tabBar1 = [XYTabBar new];
    
    [self setValue:_tabBar1 forKey:@"tabBar"];
    UIImage *image = [UIImage imageWithColor:[UIColor clearColor]];
    _tabBar1.backgroundImage = image;
//    [self.tabBar setBackgroundColor:[UIColor clearColor]];
//    [self.tabBar setBackgroundImage:image];
    _tabBar1.shadowImage = [UIImage imageWithColor:UICOLOR_WITH_RGBINT(0x999999)];
//    self.tabBar.translucent = YES;
    
    if(!IS_iPhoneX){
        _isClear = YES;
        _tabBar1.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];
        _tabBar1.shadowImage = [UIImage imageWithColor:UICOLOR_WITH_RGBINT(0x999999)];
        _tabBar1.backgroundColor = [UIColor clearColor];
    } else {
        _isClear = NO;
        _tabBar1.backgroundImage = [UIImage imageWithColor:[UIColor blackColor]];
        _tabBar1.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
        _tabBar1.backgroundColor = [UIColor blackColor];
    }
}

#pragma mark - ——————— 初始化VC ————————
-(void)setUpAllChildViewController{
    _VCS = @[].mutableCopy;
    
    RGMainPageViewController *mainVC = [[RGMainPageViewController alloc]init];
    [self setupChildViewController:mainVC title:@"首页" imageName:@"首页_03" seleceImageName:@"首页_03"];
    
    RGFocusViewController *focusVC = [[RGFocusViewController alloc]init];
    [self setupChildViewController:focusVC title:@"关注" imageName:@"首页_05" seleceImageName:@"首页_05"];
    
    RGVedioBlankViewController *videoVC = [[RGVedioBlankViewController alloc]init];
    [self setupChildViewController:videoVC title:nil imageName:@"拍摄" seleceImageName:@"拍摄"];
    
    
    RGMessageViewController *msgVC = [[RGMessageViewController alloc]init];
    [self setupChildViewController:msgVC title:@"消息" imageName:@"首页_07" seleceImageName:@"首页_07"];
    RGMeViewController *mineVC = [[RGMeViewController alloc]init];
    [self setupChildViewController:mineVC title:@"我"imageName:@"首页_09" seleceImageName:@"首页_09"];
    
    self.viewControllers = _VCS;
}

-(void)setupChildViewController:(UIViewController*)controller title:(NSString *)title imageName:(NSString *)imageName seleceImageName:(NSString *)selectImageName{
    controller.title = title;
    controller.tabBarItem.title = title;//跟上面一样效果
    if(title == nil){
        controller.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
    }else{
        controller.tabBarItem.imageInsets = UIEdgeInsetsMake(-2, 0, 2, 0);
    }
    controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0,-4);
    controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //未选中字体颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:SYSTEMFONT(13.0f)} forState:UIControlStateNormal];
    
    //选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:SYSTEMFONT(13)} forState:UIControlStateSelected];
    //包装导航控制器
    BaseNavigationViewController *nav = [[BaseNavigationViewController alloc]initWithRootViewController:controller];
    [_VCS addObject:nav];
   
    
}


-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    [_tabBar1 changeBottmLineFrameWithChosedIndex: tabBarController.selectedIndex];
    if(tabBarController.selectedIndex == 0){
        if(_isClear) {
            _tabBar1.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];
            _tabBar1.shadowImage = [UIImage imageWithColor:UICOLOR_WITH_RGBINT(0x999999)];
            _tabBar1.backgroundColor = [UIColor clearColor];
        }
    }else{
        _tabBar1.backgroundImage = [UIImage imageWithColor:[UIColor blackColor]];
        _tabBar1.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
        _tabBar1.backgroundColor = [UIColor blackColor];
    }
    //    NSLog(@"选中 %ld",tabBarController.selectedIndex);
}

-(void) changeTabbarUiStyleWithShowClear:(BOOL)isClear{
    _isClear = isClear;
    if(self.selectedIndex == 0){
        if(isClear){
            _tabBar1.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];
            _tabBar1.shadowImage = [UIImage imageWithColor:UICOLOR_WITH_RGBINT(0x999999)];
            _tabBar1.backgroundColor = [UIColor clearColor];
          
        }else{
            _tabBar1.backgroundImage = [UIImage imageWithColor:[UIColor blackColor]];
            _tabBar1.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
            _tabBar1.backgroundColor = [UIColor blackColor];
        }
    }
}

-(BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSString *token = [RGUserInfoModel getUserInfo].token;
    if([token length] == 0){
        KPostNotification(KNotificationLoginStateChange, @NO);
        return NO;
    }else{
        if([viewController isKindOfClass:[BaseNavigationViewController class]]){
            BaseNavigationViewController *nar = (BaseNavigationViewController *)viewController;
            UIViewController *vc = [[nar viewControllers] firstObject];
            if([vc isKindOfClass:[RGVedioBlankViewController class]]){
                [self presentRecordVCWhenVedioBtnClick];
                return NO;
            }
            return YES;
        }
        return YES;
    }
}

//视频tabbartItem点击拦截后，进入视频录制页面
-(void) presentRecordVCWhenVedioBtnClick{
    VideoRecordViewController *recordVC = [[VideoRecordViewController alloc]initWithConfigure:[VideoRecordConfig defaultConfigure]];
    kWeakSelf(recordVC) weakrecordVC = recordVC;
    recordVC.onRecordCompleted = ^(TXUGCRecordResult *result) {
#ifdef UGC_SMART
            const BOOL enableEdit = NO;
            void (^onEdit)(VideoPreviewViewController *previewVC) = nil;
#else
            const BOOL enableEdit = YES;
            void (^onEdit)(VideoPreviewViewController*) = ^(VideoPreviewViewController *previewVC){
                VideoCompressPreviewController *vc = [[VideoCompressPreviewController alloc] init];
                [vc setVideoPath:result.videoPath];
                [previewVC.navigationController pushViewController:vc animated:YES];
            };
#endif
            VideoPreviewViewController* previewController = [[VideoPreviewViewController alloc]
                                                             initWithCoverImage:result.coverImage
                                                             videoPath:result.videoPath
                                                             renderMode:RENDER_MODE_FILL_EDGE
                                                             showEditButton:enableEdit];
            previewController.onTapEdit = onEdit;
        [weakrecordVC.navigationController pushViewController:previewController animated:YES];
//            UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:previewController];
//            [weakrecordVC.navigationController presentViewController:nav animated:YES completion:nil];
        };
    
    BaseNavigationViewController *nar = [[BaseNavigationViewController alloc]initWithRootViewController:recordVC];
    [self presentViewController:nar animated:YES completion:nil];
}

-(void)setRedDotWithIndex:(NSInteger)index isShow:(BOOL)isShow{
    if (isShow) {
        [self.tabBar setBadgeStyle:kCustomBadgeStyleRedDot value:0 atIndex:index];
    }else{
        [self.tabBar setBadgeStyle:kCustomBadgeStyleNone value:0 atIndex:index];
    }
    
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(void) dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
